package com.anjufang.utils.page;

import com.anjufang.common.xss.SQLFilter;
import com.anjufang.utils.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * app分页
 * @author weichengyu
 *
 */
public class AppQuery extends LinkedHashMap<String, Object>  {

	private static final long serialVersionUID = 1L;
	
	// 起始数
	private int page;
	// 每页条数
	private int limit;
	
	public AppQuery(Map<String, Object> params) {
		this.putAll(params);
		
		if (StringUtils.isBlank(params.get("page")) || Integer.parseInt(params.get("page").toString()) < 1) {
			this.page = 1;
		} else {
			this.page = Integer.parseInt(params.get("page").toString());
		}
		if (StringUtils.isBlank(params.get("limit"))) {
			this.limit = 20;
		} else {
			this.limit = Integer.parseInt(params.get("limit").toString());
		}
		this.put("offset", (page-1) * limit);
		this.put("limit", limit);
		
		// 防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
		String sidx = (String) params.get("sidx");
		String order = (String) params.get("order");
		if (StringUtils.isNotBlank(sidx)) {
			this.put("sidx", SQLFilter.sqlInject(sidx));
		}
		if (StringUtils.isNotBlank(order)) {
			this.put("order", SQLFilter.sqlInject(order));
		}
	}
	
}
